﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace Parking.Models
{
    public class CompanyInfo
    {
        [Required(ErrorMessage = "El nombre de la empresa es requerido")]
        public string Name { get; set; }

        public string Phone { get; set; }
        public string Address { get; set; }
        public string Legal { get; set; }
        public string Email { get; set; }
        
        [Required(ErrorMessage = "El precio es requerido")]
        public decimal Price { get; set; }

        public CompanyInfo()
        {
            Name = "";
            Phone = "";
            Address = "";
            Legal = "";
            Email = "";
            Price = 0;
        }
        
        public CompanyInfo(string name = "", string phone = "", string address = "", string legal = "", 
            string email = "", decimal price = 0)
        {
            Name = name;
            Phone = phone;
            Address = address;
            Legal = legal;
            Email = email;
            Price = price;
        }
    }

    public interface ICompanyInfoRepository
    {
        IEnumerable<CompanyInfo> GetCompanies();
        CompanyInfo GetCompanyByName(string name);
        void InsertCompany(CompanyInfo company);
        void DeleteCompany(string name);
        void EditCompany(CompanyInfo company);
    }

    public class CompanyInfoRepository : ICompanyInfoRepository
    {
        private List<CompanyInfo> allCompanies;
        private XDocument companyData;
 
        // constructor
        public CompanyInfoRepository()
        {
            allCompanies = new List<CompanyInfo>();
 
            companyData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/CompanyInfo.xml"));
            var companies = from company in companyData.Descendants("company")
                            select new CompanyInfo(
                                company.Element("name").Value, 
                                company.Element("phone").Value,
                                company.Element("address").Value, 
                                company.Element("legal").Value,
                                company.Element("email").Value, 
                                (decimal)company.Element("price"));
            allCompanies.AddRange(companies.ToList<CompanyInfo>());
        }

        public IEnumerable<CompanyInfo> GetCompanies()
        {
            return allCompanies;            
        }

        public CompanyInfo GetCompanyByName(string name)
        {
            return allCompanies.Find(item => item.Name == name);
        }

        public void InsertCompany(CompanyInfo company)
        {
            //company.Name = (from b in companyData.Descendants("company") orderby (string)b.Element("name") descending select (string)b.Element("name")).FirstOrDefault() + 1;

            if (companyData.Root != null)
                companyData.Root.Add(
                    new XElement("company", 
                                 new XElement("name", company.Name), 
                                 new XElement("phone", company.Phone),
                                 new XElement("address", company.Address), 
                                 new XElement("legal", company.Legal), 
                                 new XElement("email", company.Email),
                                 new XElement("price", company.Price)
                        )
                    );
            companyData.Save(HttpContext.Current.Server.MapPath("~/App_Data/CompanyInfo.xml"));
        }

        public void DeleteCompany(string name)
        {
            if (companyData.Root != null)
                companyData.Root.Elements("company").Where(i => (string)i.Element("name") == name).Remove();
            companyData.Save(HttpContext.Current.Server.MapPath("~/App_Data/CompanyInfo.xml"));
        }

        public void EditCompany(CompanyInfo company)
        {
            if (companyData.Root != null)
            {
                XElement node = companyData.Root.Elements("company").FirstOrDefault(i => (string)i.Element("name") == company.Name);

                if (node != null)
                {
                    node.SetElementValue("name", company.Name);
                    node.SetElementValue("phone", company.Phone);
                    node.SetElementValue("address", company.Address);
                    node.SetElementValue("legal", company.Legal);
                    node.SetElementValue("email", company.Email);
                    node.SetElementValue("price", company.Price);
                }
            }

            companyData.Save(HttpContext.Current.Server.MapPath("~/App_Data/CompanyInfo.xml"));
        }
    }
}